
var resourceCtrl  = new XFApp.client();
XFApp.client.prototype.resource = {
	successCode : "10000",
	page : null,
	urlItems : {
		queryByPageURL : basePath+"/resource/getPage",
		delURL : basePath+"/resource/del",
		saveURL : basePath+"/resource/save",
		getByIdURL : basePath+"/resource/getById",
		approveURL : basePath+"/resource/approve"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			currentPageIndex : Number($("#currentPageIndex").val())-1,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(),
		        success: function (data, pageIndex) {
		        	$("#currentPageIndex").val(data.result.pageNo);
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data
								|| null == data.result
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	delDialog : function(id,roleCode){
		_this = this;
		webAlert({
            title:'提示',
            content:'确定要删除 '+roleCode+' 吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.doDel(id);
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	doDel : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delURL,
		    data:{id:id},
		    type:'post',
		    cache:false,
		    dataType:'json',
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		resourceCtrl.resource.queryPage();
		    	}
		     },
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }
		});
	},
	editDialog : function(id){
		_this = this;
		_this.cleanEditForm();
		if(null != id){
			$.ajax( {
			    url:_this.urlItems.getByIdURL,
			    data:{id:id},
			    type:'post',
			    cache:false,
			    dataType:'json',
			    success:function(data) {
			    	_this.fillEditForm(data.result);
			    	_this.openEditView(id);
			     },
			     error : function() {
			    	window.webDialogUtil.alert("提示","操作失败");
			     }
			});
		}else{
			$("#editView #isNeedAuth").empty();
			$("#editView #isNeedAuth").append('<option value="1" selected="selected">需要授权</option>');
			$("#editView #isNeedAuth").append('<option value="0">不需要授权</option>');
			$("#editView #isDisable").empty();
			$("#editView #isDisable").append('<option value="1" selected="selected">是</option>');
			$("#editView #isDisable").append('<option value="0">否</option>');
			_this.openEditView();
		}
	},
	cleanEditForm : function(){
		$("#editView #name").val("");
		$("#editView #url").val("");
		$("#editView #description").val("");
		$("#editView #id").val("");
	},
	fillEditForm : function(data){
		$("#editView #name").val(data.name);
		$("#editView #url").val(data.url);
		$("#editView #isNeedAuth").empty();
		if(1 == data.isNeedAuth){
			$("#editView #isNeedAuth").append('<option value="1" selected="selected">需要授权</option>');
			$("#editView #isNeedAuth").append('<option value="0">不需要授权</option>');
		}else{
			$("#editView #isNeedAuth").append('<option value="1">需要授权</option>');
			$("#editView #isNeedAuth").append('<option value="0" selected="selected">不需要授权</option>');
		}
		$("#editView #isDisable").empty();
		if(1 == data.isDisable){
			$("#editView #isDisable").append('<option value="1" selected="selected">是</option>');
			$("#editView #isDisable").append('<option value="0">否</option>');
		}else{
			$("#editView #isDisable").append('<option value="1">是</option>');
			$("#editView #isDisable").append('<option value="0" selected="selected">否</option>');
		}
		$("#editView #description").val(data.description);
		$("#editView #id").val(data.id);
	},
	openEditView : function(id){
		_this = this;
		webAlert({
            title:(null==id?"新增":"编辑"),
            content:document.getElementById("editView"),
            button:[
                    {
                    name:"保存",
                    callback:function(){
                    	_this.doSubmit(this);
                    	return false;
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	doSubmit : function(viewObj){
		_this = this;
		$.ajax( {
		    url:_this.urlItems.saveURL,
		    data:$('#editForm').serialize(),
		    type:'post',
		    cache:false,
		    dataType:'json',
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(data.isSuccess){
		    		viewObj.close();
		    		_this.queryPage();
		    	}
		     },
		     error : function() {
		    	window.webDialogUtil.alert("提示","操作失败");
		     }
		});
	},
	approve : function (option,type){
		_this = this;
		if(!_this.checkVal){
			window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");
			return ;
		}
		webAlert({
			title:'提示',
			content:'确认执行'+type+'操作吗?',
			opacity:.5,
			bg:"gray",
			width:300,
			height:10,
			button:[
				{
					name:"确定",
					callback:function(){
						_this.approveConfirm(option);
						this.close();
					}
				},{
					name:"取消",
					callback:function(){
						this.close();
					}
				}]
		});
	},
	approveConfirm: function(option){
		var index = _this.checkVal.lastIndexOf(",");
		var id = _this.checkVal.substring(0,index);
		var params = {
			op  : option, //1-审批通过 2-审批拒绝
			ids : id
		}
		var showText ;
		if(option == '1')
			showText = "审批通过" ;
		else
			showText = "审批拒绝" ;
		$.ajax({
			url :_this.urlItems.approveURL,
			data : params,
			dataType : "json",
			type : 'post',
			success : function(data) {
				if(data.code==_this.successCode){
					window.webDialogUtil.alert("提示",data.msg);
					$("[name='chkItem']:checked").each(function (){
						$("#custmAprvSts"+$(this).val()).text(showText);
					});
					//移除事件
					$("#checkAll").removeAttr("checked");
					$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
					_this.queryPage();
				}else{
					window.webDialogUtil.alert("提示",data.msg);
				}
			}
		});
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;

		$("#searchButton").on("click",function(){
			_this.queryPage();
		});
		$("#addButton").on("click",function(){
			_this.editDialog();
		});
		//审批通过
		$("#btnPass").on("click",function(){
			var chkVal = "";
			$("#page_grid input[type='checkbox']").each(function(){
				if($(this).prop("checked")){
					chkVal += $(this).val() +",";
				}
			});
			_this.checkVal = chkVal ;
			_this.approve(2,$("#btnPass").text());
		});
		//审批拒绝
		$("#btnRefuse").on("click",function(){
			var chkVal = "";
			$("#page_grid input[type='checkbox']").each(function(){
				if($(this).prop("checked")){
					chkVal += $(this).val() +",";
				}
			});
			_this.checkVal = chkVal ;
			_this.approve(1,$("#btnRefuse").text());
		});
		$("#checkAll").bind("click",function(){
			var chkVal = "";
			//全选
			if($("#checkAll").prop("checked")){
				$("[name='chkItem']").prop("checked","checked");
				$("[name='chkItem']:checkbox:checked").each(function (){
					chkVal += $(this).val()+",";
				})

			}else{
				$("[name='chkItem']").removeAttr("checked");//取消全选
				chkVal = "";
			}
			_this.checkVal = chkVal;
		});
		resourceCtrl.resource.queryPage();
	},
};
$.views.converters({
	ftime : function(value) {
		return resourceCtrl.resource.dataFormat(value) ;
	},
	fisNeedAuth : function(value) {
		switch(value){
		case 0 : return "不需要授权";
		case 1 : return "需要授权";
		default : return "未知状态";
		}
	},
	fisDisable : function(value) {
		switch(value){
			case 0 : return "否";
			case 1 : return "是";
			default : return "否";
		}
	},
	fstatus : function(value) {
		switch(value){
		case 1 : return "待审批";
		case 2 : return "审批通过";
		default : return "未知状态";
		}
	}
});
$(function(){
	resourceCtrl.resource.event();
});